﻿using System;
using System.Collections.Generic;
using NHibernate;
using NHibernate.Criterion;
using ScrumBox.Model;

namespace ScrumBox.DataAccess.Repositories
{
    /// <summary>
    /// 
    /// </summary>
    public class SprintRepository : RepositoryBase, ISprintRepository
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sessionFactory"></param>
        public SprintRepository(ISessionBuilder sessionFactory)
            : base(sessionFactory)
        {
        }

        #region ISprintRepository Members

        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectKey"></param>
        /// <returns></returns>
        public IList<Sprint> GetAllSprintsByProject(string projectKey)
        {
            var query = getSession().CreateQuery("from Sprint");
            return query.List<Sprint>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sprintKey"></param>
        /// <returns></returns>
        public Sprint GetByKey(string sprintKey)
        {
            var criteria = getSession().CreateCriteria(typeof(Sprint));
            criteria.Add(Restrictions.Eq("Key", sprintKey));
            return criteria.UniqueResult<Sprint>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Sprint Get(Guid id)
        {
            return getSession().Get<Sprint>(id);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sprint"></param>
        public void Save(Sprint sprint)
        {
            var session = getSession();
            session.SaveOrUpdate(sprint);
            session.Flush();
        }

        #endregion
    }
}